package com.mo.axion.web.controller;

import cn.hutool.core.text.CharSequenceUtil;
import com.google.common.collect.Lists;
import com.mo.axion.web.service.DataService;
import com.mo.axion.web.vo.DataParam;
import com.mo.axion.web.vo.amis.EchartsVo;
import com.mo.axion.web.vo.amis.SmsTimeLineVo;
import com.mo.axion.web.vo.amis.UserTimeLineVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Objects;

/**
 * @author Mo
 * @createTime 2024/4/13 15:35
 * @description 链路追踪
 */
@Slf4j
@RestController
@RequestMapping("/trace")
public class DataController {
    @Autowired
    DataService dataService;

    /**
     * 获取72小时内发送消息的全链路数据
     */
    @PostMapping("/message")
    public UserTimeLineVo getMessageData(@RequestBody DataParam dataParam) {
        if (Objects.isNull(dataParam) || CharSequenceUtil.isBlank(dataParam.getMessageId())) {
            return UserTimeLineVo.builder()
                    .items(new ArrayList<>()).build();
        }
        return dataService.getTraceMessageInfo(dataParam.getMessageId());
    }

    /**
     * 获取【当天】用户接收消息的全链路数据
     */
    @PostMapping("/user")
    public UserTimeLineVo getUserData(@RequestBody DataParam dataParam) {
        if (Objects.isNull(dataParam) || CharSequenceUtil.isBlank(dataParam.getReceiver())) {
            return UserTimeLineVo.builder().items(new ArrayList<>()).build();
        }
        return dataService.getTraceUserInfo(dataParam.getReceiver());
    }

    /**
     * 获取消息模板全链路数据
     */
    @PostMapping("/messageTemplate")
    public EchartsVo getMessageTemplateData(@RequestBody DataParam dataParam) {
        EchartsVo echartsVo = EchartsVo.builder().build();
        if (CharSequenceUtil.isNotBlank(dataParam.getBusinessId())) {
            echartsVo = dataService.getTraceMessageTemplateInfo(dataParam.getBusinessId());
        }
        return echartsVo;
    }

    @PostMapping("/sms")
    public SmsTimeLineVo getSmsData(@RequestBody DataParam dataParam) {
        if (Objects.isNull(dataParam) || Objects.isNull(dataParam.getDateTime()) || CharSequenceUtil.isBlank(dataParam.getReceiver())) {
            return SmsTimeLineVo.builder().items(Lists.newArrayList()).build();
        }
        return dataService.getTraceSmsInfo(dataParam);
    }

}
